/**
 * @Author: leibidong
 * @Description:用于拦截作用
 * @File:  middleHandler
 * @Version: 1.0.0
 * @Date: 2021/7/3 15:26
 */

package handler

import (
	"encoding/json"
	"ginProject/biz/dao"
	"ginProject/biz/model"
	"ginProject/biz/utils"
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis"
)

/**
 * @Author leibidong
 * @Description 拦截cookie，验证cookie，拦截处理
 * @Date  2021/7/3 16:02
 **/
func Middle(c *gin.Context) {
	cookie, err := c.Cookie("token")
	if err != nil {
		utils.ReturnFailureMessage("你还未登录，请先登录", c)
	}
	redisKey := utils.GetRedisLoginToken(cookie)
	rdb := dao.InitRedis()
	val2, err := rdb.Get(redisKey).Result()
	//反序列化
	var login model.LoginToken
	_ = json.Unmarshal([]byte((val2)), &login)
	if err == redis.Nil || err != nil {
		utils.ReturnFailureMessage("你的登录状态失效，请重新登录", c)
		c.Abort()
	} //else {
	//if(val.Status==1&&time.Now().String()<val.Expired){
	//   customer1:=model.QueryById(val.ID)
	//   utils.AddCustomer(redisKey,customer1)
	//}else {
	//    c.JSON(http.StatusForbidden,gin.H{
	//        "code": utils.ZERO,
	//        "msg":  utils.FAILURE,
	//        "data": "登录状态过期，请重新登录"})
	//}
	//}
	c.Next()
}
